package com.tianqu.tms.www.cardect.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.action.CarSearchInputSubmitAction;
import com.tianqu.tms.www.cardect.viewhelper.CarSearchInputTopViewHelper;

/**
 * 
 * 车辆搜索进入到查询结果列表页Controller
 * 
 * @author tianshifu
 * 
 */
public class CarSearchInputSubmitController extends TmsController {
	private Logger logger = Logger
			.getLogger(CarSearchInputSubmitController.class);// log

	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			// 将form信息set到entity中进行查询
			TmsDetectInfo entity = TmsDetectInfo.getInstance();

			if (uc.getAttribute(CarSearchInputSubmitAction.PAGENO) != null) {
				entity.setPageNo(Integer.parseInt((String) uc
						.getAttribute(CarSearchInputSubmitAction.PAGENO)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.MAXINDEX) != null) {
				entity.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(CarSearchInputSubmitAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.SORTKEY) != null) {
				entity.setSortKey(Integer.parseInt((String) uc
						.getAttribute(CarSearchInputSubmitAction.SORTKEY)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.ASC) != null) {
				entity.setAsc((new Boolean((String) uc
						.getAttribute(CarSearchInputSubmitAction.ASC)))
						.booleanValue());
			}

			// 在entity中进行查询
			// 这块需要添加一个状态 状态为6
			entity.setStatus(TmsDetectInfo.DETECT_DATA_EXPORT);
			List<TmsDetectInfo> list = entity.findCarInfoByCon(entity);

			CarSearchInputTopViewHelper view = null;
			List<CarSearchInputTopViewHelper> viewList = new ArrayList<CarSearchInputTopViewHelper>();
			// 将list数据set到viewhelper中
			if (list.size() > 0) {

				for (int i = 0; i < list.size(); i++) {
					TmsDetectInfo dataEntity = (TmsDetectInfo) list.get(i);
					view = new CarSearchInputTopViewHelper();
					setViewFromEntity(view, dataEntity);

					view.setCssFlag(dataEntity.getSeeBsflag());

					viewList.add(view);
				}
			}

			uc.setAttribute("viewList", viewList);
			uc.setAttribute(PAGING, entity);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

	/**
	 * 
	 * 将查询得到的数据set到viewhelp中，需要重构数据
	 * 
	 * @throws TmsDaoException
	 */
	private CarSearchInputTopViewHelper setViewFromEntity(
			CarSearchInputTopViewHelper view, TmsDetectInfo entity)
			throws TmsDaoException {
		DectCodes code = DectCodes.getInstance();
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));// 将毫升转化成升
		Codes tmsCode = Codes.getInstance();
		if (entity.getColor() == null || "".equals(entity.getColor())) {
			view.setColor("");
		} else {
			view.setColor(tmsCode.getItemValueByItemIdAndItemCode(
					Codes.VEHICLE_COLOR, Integer.parseInt(entity.getColor())));
		}

		view.setGearbox(code.getItemValueByItemIdAndItemCode(
				DectCodes.GEARBOX_TYPE, String.valueOf(entity.getGearbox())));
		// view.setSource(String.valueOf(entity.getSource()));//原始数据
		// view.setSourceName(code.getItemValueByItemIdAndItemCode(DectCodes.CAR_SOURSE,
		// String.valueOf(entity.getSource())));
		view.setChkModel(String.valueOf(entity.getChkModel()));// 原始数据
		view.setChkModelName(code.getItemValueByItemIdAndItemCode(
				DectCodes.DETECT_MODE, String.valueOf(entity.getChkModel())));
		if (view.getChkModelName() == null || "".equals(view.getChkModelName())) {
			view.setChkModelName("-");
		}

		view.setStatus(String.valueOf(entity.getStatus()));// 原始数据

		view.setStatusName("已完成");

		view.setUpdateTime(entity.getUpdateTime());// 已经处理完的时间

		view.setScopeNum(entity.getScopeNum());

		return view;
	}

}
